<?php


namespace app\api\model;


class Product extends BaseModel {
    protected $hidden = ['delete_time','update_time','category_id','pivot','create_time','from'];

    //将图片路径写完整
    public function getMainImgUrlAttr($url,$data){
        return $this->imgUrl($url,$data);
    }

    //获取商品详图和商品是一对多的关系
    public function imgs(){
        return $this->hasMany('ProductImage','product_id','id');
    }

    //获取商品的参数
    public function properties(){
        return $this->hasMany('ProductProperty','product_id','id');
    }

    //获取商品
    public static function getMostRecent($count){
        $list = self::limit($count)->order('create_time','desc')->select();
        return $list;
    }

    //获取商品详情
    public static function getOneProductDetails($id){
       // $list = self::with(['properties','imgs.imgUrls'])->find($id);
//        $list = db('product')->where('id',$id)->find();
//        $list['productImg'] = db('image')->where('id',$list['img_id'])->find();
        //采用闭包完成排序,关联模型下的关联的属性进行排序
        $list = self::with(['properties'])->with([
            'imgs'=>function($query){
                    $query->with('imgUrls')->order('order','asc');
            }]
        )->find($id);

        return $list;
    }

    //获取分类下的商品
    public static function getAllProductByCategory($id){
        $list = self::where('category_id',$id)->select();
        return $list;
    }




}




